<template>

  <el-form ref="form_add" :model="form" :rules="rules" label-width="130px">
    <el-card class="box-card">
      <div slot="header">
        <span>开学信息登记</span>
        <el-button-group style="float: right; margin-bottom: 5px; ">
          <el-button @click="addItem">添加年级</el-button>
          <el-button type="primary" @click="addInfo">保存</el-button>
        </el-button-group>
      </div>

      <el-row>
        <el-col :span="7">
          <el-form-item label="登记日期" prop="regDate">
            <el-date-picker type="date" readonly format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="选择登记日期"
              v-model="form.regDate"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="单位" prop="schoolName">
            <el-select v-model="form.schoolName" filterable placeholder="请选择单位" @change="loadInfo">
              <el-option-group v-for="group in options" :key="group.label" :label="group.label">
                <el-option v-for="item in group.options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-option-group>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="教职工复工人数">
            <el-input-number v-model="form.teacherNum" :min="1" :max="1000" label="教职工复工人数"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>



      <div v-for="(item,index) in form.edus" :key="index" :index="index" :item="item" class="el-card">
        <el-card class="box-card" style="margin-bottom: 5px;">
          <el-row>
            <el-col :span="7">
              <el-form-item label="年级">
                <el-select v-model="item.gradeName" placeholder="请选择年级">
                  <el-option v-for="tm in selection" :key="tm.value" :label="tm.label" :value="tm.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="巳复学班级数">
                <el-input-number v-model="item.classNum" :min="1" :max="1000" label="请填写人数"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="学生实到数">
                <el-input-number v-model="item.factStdentNum" :min="1" :max="1000" label="请填写人数"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="3" v-show="index!=0">
              <el-button style="float: right; padding: 3px 0" type="text" @click="remveItem(index)">移除</el-button>
            </el-col>
          </el-row>


          <el-collapse v-model="activeNames">
            <el-collapse-item title="学生应到数" name="1">
              <el-row style="margin-bottom: 5px;">
                <el-col :span="8">
                  <el-form-item label="2019年秋季定案数">
                    <el-input-number v-model="item.planStdentNum" :min="1" :max="1000" label="请填写人数"></el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="8">

                  <el-form-item label="转入">
                    <el-input-number v-model="item.comeinNum" :min="1" :max="1000" label="请填写人数"></el-input-number>
                  </el-form-item>
                </el-col>

                <el-col :span="8">

                  <el-form-item label="转出">
                    <el-input-number v-model="item.comeoutNum" :min="1" :max="1000" label="请填写人数"></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">

                  <el-form-item label="事假">
                    <el-input-number v-model="item.absentNum" :min="1" :max="1000" label="请填写人数"></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-collapse-item>
            <el-collapse-item title="因病缺课、发热等异常人员追踪诊断情况" name="2">

              <el-row style="margin-bottom: 5px;">
                <el-col :span="8">
                  <el-form-item label="疑似新冠肺炎人数">
                    <el-input-number v-model="item.suspectedNum" :min="1" :max="1000" label="请填写人数"></el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="8">

                  <el-form-item label="其他传染病人数">
                    <el-input-number v-model="item.otherInfectNum" :min="1" :max="1000" label="请填写人数"></el-input-number>
                  </el-form-item>
                </el-col>

                <el-col :span="8">

                  <el-form-item label="其他疾病人数">
                    <el-input-number v-model="item.otherCaseNum" :min="1" :max="1000" label="请填写人数"></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">

                  <el-form-item label="待诊断人数">
                    <el-input-number v-model="item.infectionNum" :min="1" :max="1000" label="请填写人数"></el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="8">

                  <el-form-item label="居家">
                    <el-input-number v-model="item.stayNum" :min="1" :max="1000" label="请填写人数"></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>

            </el-collapse-item>


            <el-collapse-item title="备注" name="3">
              <el-input type="textarea" v-model="item.remark"></el-input>
            </el-collapse-item>
          </el-collapse>


        </el-card>
      </div>





      <el-form-item style="margin-top: 5px;">

        <el-button-group style="float: right; margin-bottom: 5px; ">
          <el-button @click="addItem">添加年级</el-button>
          <el-button type="primary" @click="addInfo">保存</el-button>
        </el-button-group>
      </el-form-item>
    </el-card>
  </el-form>

</template>

<script>
  import {
    jsonData
  } from '@api/demo.jsondata.table.1'
  import {
    jsonData2
  } from '@api/demo.jsondata.table.2'
  import axios from 'axios'
  export default {
    mounted() {
      this.options = jsonData();
      this.classMap = jsonData2();
      this.getCurrentDate();
      this.loadInfo();
    },
    data() {
      return {
        form: {
          regDate: '',
          schoolName: '',
          teacherNum: 0,
          typeName: '',
          edus: [{
            gradeName: '',
            classNum: 0,
            factStdentNum: 0,
            planStdentNum: 0,
            comeinNum: 0,
            comeoutNum: 0,
            absentNum: 0,
            suspectedNum: 0,
            otherInfectNum: 0,
            otherCaseNum: 0,
            infectionNum: 0,
            stayNum: 0,
            remark: ''
          }]
        },
        rules: {
          schoolName: [{
            required: true,
            message: '请选择一个日期',
            trigger: 'change'
          }],
          regDate: [{
            required: true,
            message: '请选择单位',
            trigger: 'change'
          }],
          gradeName: [{
            required: true,
            message: '请选择年级',
            trigger: 'change'
          }]
        },
        options: [],
        classMap: [],
        computedLenth: 0,
        activeNames: []
      }
    },
    computed: {
      selection: function() {
        let map = this.classMap;
        let this_ = this;
        let value_ = this.form.schoolName;
        let res_ = [];
        this.options.forEach(function(v, i) {

          v.options.forEach(function(k, j) {
            if (k.value == value_) {
              res_ = map[v.label];
              this_.form.typeName = v.label;
            }
          })
        })
        this.computedLenth = res_.length
        return res_;
      }
    },
    methods: {

      getCurrentDate() {
        var date = new Date()
        var month = parseInt(date.getMonth() + 1)
        var day = date.getDate()
        if (month < 10) month = '0' + month
        if (day < 10) day = '0' + day
        this.form.regDate = date.getFullYear() + '-' + month + '-' + day
      },
      loadInfo() {

        axios.get('http://localhost:11003/manager/api/student/load-register', {
            params: {
              regDate: this.form.regDate,
              schoolName: this.form.schoolName
            }
          })
          .then(res => {})
      },
      addInfo() {
        this.$refs.form_add.validate((valid) => {
          if (valid) {
            axios.post('http://localhost:11003/manager/api/student/add-register', this.form)
              .then(res => {

                console.log("====", res.status, res.data.flag)
                if (res.status == 200 && res.data.flag) {
                  this.$notify.info({
                    title: '操作成功',
                    message: '保存成功'
                  })
                } else {

                  this.$notify.error({
                    title: '错误',
                    message: '请求异常'
                  })
                }
              })
          } else {
            this.$notify.error({
              title: '错误',
              message: '校验失败'
            })
            return false
          }
        })
      },
      remveItem(i) {
        if (i !== 0) {
          this.form.edus.splice(i, 1)
        }
      },
      addItem() {

        this.$refs.form_add.validate((valid) => {
          if (valid) {
            if (this.form.edus.length < this.computedLenth)
              this.form.edus.push({
                gradeName: '',
                typeName: '',
                classNum: 0,
                factStdentNum: 0,
                planStdentNum: 0,
                comeinNum: 0,
                comeoutNum: 0,
                absentNum: 0,
                suspectedNum: 0,
                otherInfectNum: 0,
                otherCaseNum: 0,
                infectionNum: 0,
                stayNum: 0,
                remark: 0
              })
          } else {
            this.$notify.error({
              title: '错误',
              message: '校验失败'
            })
            return false
          }
        })

      }
    }

  }
</script>

<style>
</style>
